***************** Replication of Franchino, Fabio, and Camilla Mariotto. “Bargaining Outcomes and Success in EU Economic Governance Reforms”. Political Science Research and Methods.

* Step 1 to reproduce Figure A6

*************   EXPLAINING COUNTRY BARGAINING SUCCESS
******************************* SENSITIVITY ANALYSIS  - Add Gaussian random noise to positions
clear all
version 16
pause on

use conflict_data1.dta, clear
*Two-level linear regression of success on x1...n with random intercepts issue_id, and robust standard errors
meglm c.success c.distSQ c.distCOM c.distPRES c.distEP i.ep_rule i.ep_rule#c.distEP c.extreme c.v_power c.salience_r c.pop c.gdp_pc if vot_right==2 || issue_id: , vce(robust)

* saving betas and v
matrix b = get(_b)
matrix list b
svmat b, names(b)
keep b*
drop if b1==.

gen v=. /* added */

save betas_noise.dta, replace

local v = 0.1  /* v is s.d of Gaussian noise - s.d. of country and institutional positions is 41.74882 */
while `v' <= 42.1 {

clear all
version 16
pause on

use conflict_data1.dta, clear
*Two-level linear regression of success on x1...n with random intercepts issue_id, and robust standard errors
meglm c.success c.distSQ c.distCOM c.distPRES c.distEP i.ep_rule i.ep_rule#c.distEP c.extreme c.v_power c.salience_r c.pop c.gdp_pc if vot_right==2 || issue_id: , vce(robust)

* saving betas and v
matrix b = get(_b)
matrix list b
svmat b, names(b)
keep b*
drop if b1==.

gen v=. /* added */

save betas2.dta, replace

set seed 1234

local m = 1 /* iteration per each s.d. value */
while `m' <= 1000 {
	
* upload original dataset
use conflict_data1.dta, clear
* drop position-related variables
drop success cntry_posAVE extreme distCOM distEP distSQ pos_pres_fin distPRES
sort country issue_id

* Add Gaussian noise N(0,v) to Commission positions
generate random = rnormal(0,`v') if country==1 /* one random noise per issue */
bysort issue_id: egen random_max = max(random)
drop random
rename random_max random
replace comm=comm+random 
replace comm=100 if comm>100 
replace comm=0   if comm<0 

* Add Gaussian noise N(0,v) to Parliament positions
replace random = rnormal(0,`v') if country==1 /* one random noise per issue */
bysort issue_id: egen random_max = max(random)
drop random
rename random_max random
replace ep_1=ep_1+random
replace ep_1=100 if ep_1>100 
replace ep_1=0   if ep_1<0 

* Add Gaussian noise N(0,v) to country positions
replace random = rnormal(0,`v')
replace cntry_pos=cntry_pos+random
replace cntry_pos=100 if cntry_pos>100 
replace cntry_pos=0   if cntry_pos<0 

****** DEPENDENT VARIABLE
gen success=-abs(cntry_pos-outcome) if vot_right==2
label variable success "Bargaining success =-abs(cntry_pos-outcome)"

****** INDEPENDENT VARIABLES
*****  Extremity of position
* compute mean country position
gen cntry_posAVE=.
label variable cntry_posAVE "Mean of positions of countries with voting rights"
local i = 1
while `i' <= 35 {
sum cntry_pos if issue_id== `i' & vot_right==2
replace cntry_posAVE=r(mean) if issue_id== `i' & vot_right==2
local i = `i' + 1
}
gen extreme=abs(cntry_pos-cntry_posAVE)
label variable extreme "Extremity of country position =abs(cntry_pos-cntry_posAVE)"

*****  Distance to Commission position
gen distCOM=abs(cntry_pos-comm) if vot_right==2
label variable distCOM "Distance to Commission position =abs(cntry_pos-comm)"

*****  Distance to Parliament position
gen distEP=abs(cntry_pos-ep_1) if vot_right==2
label variable distEP "Distance to EP position =abs(cntry_pos-ep_1)"

*****  Distance to SQ (sq is anchored at 0)
gen distSQ=cntry_pos if vot_right==2
label variable distSQ "Distance to SQ =cntry_pos"

*****  Finalizing Council president
* position of finalizing country president
gen pos_pres_fin=.
label variable pos_pres_fin "Position of finalizing presidency country"
local i = 1
while `i' <= 35 {
sum cntry_pos if issue_id== `i' & cou_pres_fin==2
replace pos_pres_fin=r(mean) if issue_id== `i'
local i = `i' + 1
}
* we set the positions of finalizing country presidents in these issues since they have no voting rights
replace pos_pres_fin=0 if issue_id==14 | issue_id==15 | issue_id==21
replace pos_pres_fin=50 if issue_id==12 | issue_id==28 | issue_id==29
replace pos_pres_fin=100 if issue_id==13 | issue_id==20 | issue_id==25 | issue_id==26 | issue_id==27 | issue_id==33 | issue_id==35
gen distPRES=abs(cntry_pos-pos_pres_fin) if vot_right==2
label variable distPRES "Distance to Fin Council Pres. position =abs(cntry_pos-pos_pres_fin)"

********* Two-level linear regression of success on x1...n with random intercepts issue_id, and robust standard errors
meglm c.success c.distSQ c.distCOM c.distPRES c.distEP i.ep_rule i.ep_rule#c.distEP c.extreme c.v_power c.salience_r c.pop c.gdp_pc || issue_id: , vce(robust)

********* Saving betas
matrix b = get(_b)
matrix list b
svmat b, names(b)
keep b*
drop if b1==.

gen v=`v' /* added */

save b.dta, replace
use betas2.dta, clear
append using b.dta
save betas2.dta, replace

local m = `m' + 1 /* simulations */
}

* drop obs from previous simulation
drop if v==.
drop if v==`v' - 1

* compute percentiles and mean
local i = 1
while `i' <= 16 {
egen b`i'_u = pctile(b`i'), p(97.5)
egen b`i'_l = pctile(b`i'), p(2.5)
local i = `i' + 1
}
collapse (mean) b1-b16_l

save betas2.dta, replace
use betas_noise.dta, clear
append using betas2.dta
save betas_noise.dta, replace

local v = `v' + 1 /* increments of s.d. */
}

drop if v==.

order v

rename b1	distSQ
rename b2   distCOM
rename b3	distPRES
rename b4	distEP
rename b5	ep_rule_1
rename b6	ep_rule_2
rename b7	ep_rule_distEP_1b
rename b8	ep_rule_distEP_2
rename b9	extreme
rename b10	v_power
rename b11	salience_r
rename b12	pop
rename b13	gdp_pc
rename b14	cons
rename b15	var_issue_id
rename b16	var_success

rename b1_u	distSQ_u
rename b2_u   distCOM_u
rename b3_u	distPRES_u
rename b4_u	distEP_u
rename b5_u	ep_rule_1_u
rename b6_u	ep_rule_2_u
rename b7_u	ep_rule_distEP_1b_u
rename b8_u	ep_rule_distEP_2_u
rename b9_u	extreme_u
rename b10_u	v_power_u
rename b11_u	salience_r_u
rename b12_u	pop_u
rename b13_u	gdp_pc_u
rename b14_u	cons_u
rename b15_u	var_issue_id_u
rename b16_u	var_success_u

rename b1_l	distSQ_l
rename b2_l   distCOM_l
rename b3_l	distPRES_l
rename b4_l	distEP_l
rename b5_l	ep_rule_1_l
rename b6_l	ep_rule_2_l
rename b7_l	ep_rule_distEP_1b_l
rename b8_l	ep_rule_distEP_2_l
rename b9_l	extreme_l
rename b10_l	v_power_l
rename b11_l	salience_r_l
rename b12_l	pop_l
rename b13_l	gdp_pc_l
rename b14_l	cons_l
rename b15_l	var_issue_id_l
rename b16_l	var_success_l

save betas_noise.dta, replace